<div id="tabs2" style="padding: 10px; width: 45%">
	<h3>Manage Products</h3>
	<hr>
	<div class="container">
		<div id="objects">&nbsp;</div>
		<div class="span-24">&nbsp;</div>
		<div id="objects_input">
			<input type="text" name="obj_in" id="obj_in"
				style="width: 500px; padding: 5px; margin-right: 10px;" />
			<div id="sel-cat"></div>
			<button id="obj_sub" value="Add"
				style="padding: 5px; margin-right: 10px;">Add Object</button>
			<button id="obj_sub_update" value="Add"
				style="padding: 5px; margin-right: 10px;">Update Object</button>

		</div>
	</div>
</div>




<script type="text/javascript">

//Objects

function loadObjects(){

$.getJSON('index.php/xmlrpc_client/get_categories',function(data){

	    var text='<select id="catselect"><option value="-1">Select Category</option>';
		$.each(data, function(key, val) {
		      text=text+'<option value="'+val.id+'" >'+val.name+'</option>';
		});	
		text=text+'</select>';

		$('#sel-cat').html(text);

	});
	
	   $.getJSON('index.php/xmlrpc_client/get_all_objects', function(data) {
	
		   console.log("Loading custom objects");
		   var items = [];
	       var text='<div class="tobjects">';

		      var i=1;
		   $.each(data, function(key, val) {
		      text=text+'<ul objid="'+val.id+'" class="objdiv ui-widget ui-helper-clearfix">'
		     +'<li class="ui-state-default ui-corner-all editobj" ><span class="ui-icon ui-icon-pencil"></span></li>'
		      +'<li class="ui-state-default ui-corner-all delobj" ><span class="ui-icon ui-icon-trash"></span></li>'
		      +'<li style="width:30%" class="namebox"> '+val.name+'</li>'
		      +'<li class="namebox"> '+val.catname+'</li></ul>'
		      
		      ;

		      i++;
		   });
		   text=text+'</div>';

		   $('#objects').html(text);
		   $('#obj_sub').show();
		   $('#obj_sub_update').hide();
		   $('#obj_in').val('');
		   associateObjectActions();

	   });
}

function associateObjectActions(){



	$('.editobj').each(function(){
		
		$(this).click(function(){

			
			inobj=$(this);
			ulobj=inobj.parent();
			nameobj=ulobj.children(".namebox");

			console.log(nameobj.text());
			
			$('#obj_in').val(nameobj.text());
			$('#obj_sub_update').attr('objid',ulobj.attr('objid'));

			$('#obj_sub').hide();
			
			$('#obj_sub_update').show();
			
		});
	});
	
	$('.delobj').each(function(){
		$(this).click(function(){

			inobj=$(this);
			ulobj=inobj.parent();
			objid=ulobj.attr('objid');

			//Post and delete
			var dataString='&objid='+objid;

			$.ajax({
			type: "POST",
				url: "index.php/xmlrpc_client/delete_object",
				data: dataString,
				success : function(data){
					$.jGrowl("Object deleted.!");
					loadObjects();
				}
			});			
			
		});
	});
	
}


$(document).ready(function(){
	loadObjects();

	$('#obj_sub').button().click(function(){
		var text=$('#obj_in').val();
		if(text != "" ){
			var dataString='&objname='+text+'&catid='+$('#catselect').val();
			console.log('Making ajax call');
			$.ajax({
				type: "POST",
					url: "index.php/xmlrpc_client/create_object",
					data: dataString,
					success : function(data){
						loadObjects();
						$.jGrowl("Object created.!");
					}
			});
		}
		
	});

	$('#obj_sub_update').button().click(function(){
		var text=$('#obj_in').val();
		var objid=$(this).attr('objid');

		if(text != "" ){
			var dataString='&name='+text+'&objid='+objid+'&catid='+$('#catselect').val();
			$.ajax({
				type: "POST",
					url: "index.php/xmlrpc_client/update_object",
					data: dataString,
					success : function(data){
						loadObjects();
						$.jGrowl("Object created.!");
					}
			});
		}
		
	});
});
</script>
